package com.hospital.doctor.controller;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.hospital.doctor.pojo.Doctor;
import com.hospital.doctor.service.LoginService;
import com.hospital.doctor.utils.JWTUtils;
import com.sun.javafx.geom.transform.SingularMatrixException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author QIUYANG
 * @title: LoginController
 * @description: TODO
 * @date 2021/5/611:10
 */
@RestController
@CrossOrigin
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/do")
    public Map<String,Object> login(@RequestBody Doctor doctor){
        Map<String,Object> map = new HashMap<>();
        Doctor user = loginService.login(doctor);
        if (user!=null&& user.getdPassword().equals(doctor.getdPassword())){
            Map<String,String> payload = new HashMap<>();
            payload.put("id",""+user.getdId());
            payload.put("name",user.getdName());
            System.out.println("=========");
            System.out.println(user.getkId());
            System.out.println("=========");

            payload.put("kid",""+user.getkId());
            //生成token
            System.out.println("-----------------------");
            System.out.println(user.getkId());
            System.out.println(user.getdName());
            System.out.println(user.getkId());
            String token = JWTUtils.getToken(payload);
            map.put("token",token);
            map.put("msg","登陆成功");
            map.put("dName",user.getdName());
            System.out.println("登录成功");
            return map;
        }else {
            map.put("msg","账号或密码错误！");
            System.out.println("错误");
            return map;
        }
        }


    @PostMapping("/test")
    public Map<String,Object> test() {
        Map<String, Object> map = new HashMap<>();
        map.put("msg","sgsgs");
        return map;

    }
}
